package com.gscitysfy.cus.drain.modules.pipe.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Maps;
import com.gscitysfy.cloudfrmwk.assistant.domain.Result;
import com.gscitysfy.cloudfrmwk.data.mybatis.HerodotusIdentifierGenerator;
import com.gscitysfy.cloudfrmwk.security.utils.SecurityUtils;
import com.gscitysfy.cus.drain.modules.inspection.vo.AppQueryVo;
import com.gscitysfy.cus.drain.modules.inspection.vo.MiniProgramCountVo;
import com.gscitysfy.cus.drain.modules.pipe.entity.DrainPipeNetworkFlaw;
import com.gscitysfy.cus.drain.modules.pipe.entity.DrainPipeNetworkJob;
import com.gscitysfy.cus.drain.modules.pipe.entity.DrainPipeNetworkPage;
import com.gscitysfy.cus.drain.modules.pipe.entity.DrainPipeNetworkReport;
import com.gscitysfy.cus.drain.modules.pipe.mapper.*;
import com.gscitysfy.cus.drain.modules.pipe.service.IDrainPipeNetworkJobService;
import com.gscitysfy.cus.drain.modules.pipe.vo.DrainPipeNetworkJobVo;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.util.Asserts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import static com.gscitysfy.cus.drain.modules.inspection.service.impl.DrainInspectionMnOrderServiceImpl.PLATFORM_ADMIN;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author admin
 * @since 2022-11-15 10:50:04
 */
@Service
public class DrainPipeNetworkJobServiceImpl extends ServiceImpl<DrainPipeNetworkJobMapper, DrainPipeNetworkJob> implements IDrainPipeNetworkJobService {
    @Resource
    HerodotusIdentifierGenerator herodotusIdentifierGenerator;

    @Autowired
    DrainPipeNetworkJobMapper drainPipeNetworkJobMapper;

    @Autowired
    DrainPipeNetworkPlanMapper drainPipeNetworkPlanMapper;

    @Autowired
    DrainPipeNetworkPageMapper drainPipeNetworkPageMapper;

    @Autowired
    DrainPipeNetworkFlawMapper drainPipeNetworkFlawMapper;

    @Autowired
    DrainPipeNetworkReportMapper drainPipeNetworkReportMapper;

    @Override
    public Result<Object> getByPage(Page<DrainPipeNetworkJob> page, DrainPipeNetworkJobVo drainPipeNetworkJobVo) {
        LambdaQueryWrapper<DrainPipeNetworkJob> queryWrapper = new LambdaQueryWrapper<>();
        drainPipeNetworkJobMapper.selectList(null);


        if (StringUtils.isNotBlank(drainPipeNetworkJobVo.getCmJobPipeType())) {
            queryWrapper.eq(DrainPipeNetworkJob::getCmJobPipeType, drainPipeNetworkJobVo.getCmJobPipeType());
        }
        if (Objects.nonNull(drainPipeNetworkJobVo.getCmJobStatus())) {
            queryWrapper.eq(DrainPipeNetworkJob::getCmJobStatus, drainPipeNetworkJobVo.getCmJobStatus());
        }

        if (Objects.nonNull(drainPipeNetworkJobVo.getCmStartDate())) {
            queryWrapper.ge(DrainPipeNetworkJob::getCmJobStartDate, drainPipeNetworkJobVo.getCmStartDate());
        }

        if (Objects.nonNull(drainPipeNetworkJobVo.getCmEndDate())) {
            queryWrapper.le(DrainPipeNetworkJob::getCmJobStartDate, drainPipeNetworkJobVo.getCmEndDate());
        }
        if (StringUtils.isNotBlank(drainPipeNetworkJobVo.getCmJobNo())) {
            queryWrapper.like(DrainPipeNetworkJob::getCmJobNo, drainPipeNetworkJobVo.getCmJobNo());
        }
        if (StringUtils.isNotBlank(drainPipeNetworkJobVo.getCmJobName())) {
            queryWrapper.like(DrainPipeNetworkJob::getCmJobName, drainPipeNetworkJobVo.getCmJobName());
        }
        Page<DrainPipeNetworkJob> page1 = this.page(page, queryWrapper);
        List<DrainPipeNetworkJob> records = page1.getRecords();


        records.forEach(e -> {
            LambdaQueryWrapper<DrainPipeNetworkPage> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(DrainPipeNetworkPage::getCmJobId, e.getCmKid());
            List<DrainPipeNetworkPage> drainPipeNetworkPages = drainPipeNetworkPageMapper.selectList(queryWrapper1);
            drainPipeNetworkPages.forEach(l -> {
                long count1 = drainPipeNetworkPages.stream().filter(e1 -> e1.getCmPipeDetectionStatus() == 0).count();
                long count2 = drainPipeNetworkPages.stream().filter(e2 -> e2.getCmPipeDetectionStatus() == 1).count();
                if (0 == count1 && 0 == count2) {
                    e.setCmJobStatus(2);
                }
            });
        });

        return Result.content(page1.setRecords(records));

    }

    @Override
    public DrainPipeNetworkJob detail(Long id) {
        Asserts.notNull(id, "problemId");
        DrainPipeNetworkJob drainPipeNetworkJob = drainPipeNetworkJobMapper.selectById(id);
        if (Objects.nonNull(drainPipeNetworkJob.getCmPlanId())) {
            drainPipeNetworkJob.setDrainPipeNetworkPlan(drainPipeNetworkPlanMapper.selectById(drainPipeNetworkJob.getCmPlanId()));
        }
        LambdaQueryWrapper<DrainPipeNetworkPage> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DrainPipeNetworkPage::getCmJobId, id);
        List<DrainPipeNetworkPage> drainInspectionRlTracks = drainPipeNetworkPageMapper.selectList(queryWrapper);
        if (CollectionUtils.isNotEmpty(drainInspectionRlTracks)) {
            drainPipeNetworkJob.setDrainPipeNetworkPage(drainInspectionRlTracks);
        }
        return drainPipeNetworkJob;
    }

    @Override
    public Result<Object> jobXcx(Page<DrainPipeNetworkJob> page, String stutas, String keyWord) {
        LambdaQueryWrapper<DrainPipeNetworkJob> queryWrapper = new LambdaQueryWrapper<>();
        //当前任务登录人员
        String name = SecurityUtils.getPrincipal().getName();
        if (com.baomidou.mybatisplus.core.toolkit.StringUtils.isNotEmpty(name) && !PLATFORM_ADMIN.equals(name)) {
            queryWrapper.eq(StringUtils.isNotEmpty(name), DrainPipeNetworkJob::getCmJobPeople, name);
        }
        //搜索条件
        if (StringUtils.isNotBlank(keyWord)) {
            queryWrapper.and(e ->
                    e.like(DrainPipeNetworkJob::getCmJobNo, keyWord).or()
                            .like(DrainPipeNetworkJob::getCmJobName, keyWord)
            );
        }
        if (!"null".equals(stutas)) {
            queryWrapper.eq(DrainPipeNetworkJob::getCmJobStatus, Integer.parseInt(stutas));
        }


        List<DrainPipeNetworkJob> records = this.page(page, queryWrapper).getRecords();
        records.forEach(e -> {
            LambdaQueryWrapper<DrainPipeNetworkPage> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(DrainPipeNetworkPage::getCmJobId, e.getCmKid());
            List<DrainPipeNetworkPage> drainPipeNetworkPages = drainPipeNetworkPageMapper.selectList(queryWrapper1);
            drainPipeNetworkPages.forEach(l -> {
                long count1 = drainPipeNetworkPages.stream().filter(e1 -> e1.getCmPipeDetectionStatus() == 0).count();
                long count2 = drainPipeNetworkPages.stream().filter(e2 -> e2.getCmPipeDetectionStatus() == 1).count();
                if (0 == count1 && 0 == count2) {
                    e.setCmJobStatus(2);
                }
            });

        });
        return Result.content(records);
    }


    @Override
    public Result<Object> detailXcxGw(Long id,String  orderState) {
        LambdaQueryWrapper<DrainPipeNetworkPage> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DrainPipeNetworkPage::getCmJobId, id);
        if (!"null".equals(orderState)) {
            queryWrapper.eq(DrainPipeNetworkPage::getCmPipeDetectionStatus, Integer.parseInt(orderState));
        }
        return Result.content(drainPipeNetworkPageMapper.selectList(queryWrapper));
    }

    @Override
    public Boolean addReport(List<DrainPipeNetworkReport> drainPipeNetworkReports) {
        String name = SecurityUtils.getPrincipal().getName();
        drainPipeNetworkReports.forEach(e -> {
            e.setCmCreator(name);
            e.setCmKid(herodotusIdentifierGenerator.nextId(e).longValue());
            drainPipeNetworkReportMapper.insert(e);
        });
        return true;
    }

    @Override
    public Result<Object> listReport() {
        return Result.content(drainPipeNetworkReportMapper.selectList(null));
    }

    @Override
    public Map<String, Object> detailFlawByPipeId(Long id) {
        Map map = Maps.newHashMap();
        LambdaQueryWrapper<DrainPipeNetworkFlaw> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DrainPipeNetworkFlaw::getCmPipeId, id);
        List<DrainPipeNetworkFlaw> drainPipeNetworkFlaws = drainPipeNetworkFlawMapper.selectList(queryWrapper);
//        List<String> picList = Lists.newArrayList();
//        List<String> videoList = Lists.newArrayList();
//        List<String> reportList = Lists.newArrayList();
//        drainPipeNetworkFlaws.forEach(e -> {
//            if (StringUtils.isNotBlank(e.getCmFileImgId())) {
//                picList.add(e.getCmFileImgId());
//            }
//            if (StringUtils.isNotBlank(e.getCmFileVideoId())) {
//                videoList.add(e.getCmFileVideoId());
//            }
//            if (StringUtils.isNotBlank(e.getCmFileReportId())) {
//                reportList.add(e.getCmFileReportId());
//            }
//
//        });
        map.put("drainPipeNetworkFlaws", drainPipeNetworkFlaws);
//        map.put("videoList", videoList);
//        map.put("reportList", reportList);
        return map;
    }

    @Override
    public Boolean startAndFinishPipe(Long id, Integer status,Long jobId) {
        if(status==1){
            DrainPipeNetworkJob drainPipeNetworkJob = drainPipeNetworkJobMapper.selectById(jobId);
            if(drainPipeNetworkJob.getCmJobStatus()!=1){
            drainPipeNetworkJob.setCmJobStatus(1);
            drainPipeNetworkJobMapper.updateById(drainPipeNetworkJob);}
        }
        DrainPipeNetworkPage drainPipeNetworkPage = drainPipeNetworkPageMapper.selectById(id);
        drainPipeNetworkPage.setCmPipeDetectionStatus(status);
        int i = drainPipeNetworkPageMapper.updateById(drainPipeNetworkPage);
        if (i >= 0) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public Boolean startAndFinishJob(Long id, Integer status) {
        DrainPipeNetworkJob drainPipeNetworkJob = drainPipeNetworkJobMapper.selectById(id);
        drainPipeNetworkJob.setCmJobStatus(status);
        int i = drainPipeNetworkJobMapper.updateById(drainPipeNetworkJob);
        if (i >= 0) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public MiniProgramCountVo countPipeTask() {
        AppQueryVo queryVo = new AppQueryVo();
        String name = SecurityUtils.getPrincipal().getName();
        String userId = SecurityUtils.getPrincipal().getUserId();
        if (StringUtils.isNotEmpty(name) && !PLATFORM_ADMIN.equals(name)) {
            queryVo.setUserId(userId);
        }
        //管网检测任务
        MiniProgramCountVo pipeTaskVo = new MiniProgramCountVo();
        Long tempIncrCnt = drainPipeNetworkJobMapper.countPipeTask(queryVo);
        pipeTaskVo.setBasisName("管网检测任务");
        pipeTaskVo.setCnt(tempIncrCnt);
        return pipeTaskVo;
    }




}
